<%--
  Created by IntelliJ IDEA.
  User: 494418174
  Date: 2020/3/17 0017
  Time: 18:39
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta http-equiv="Access-Control-Allow-Origin" content="*">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" href="/layui/css/layui.css">
</head>
<body class="childrenBody">
<!-- 搜索条件开始 -->
<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
    <legend>查询条件</legend>
</fieldset>
<form class="layui-form" method="post" id="searchFrm">
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">管理员姓名:</label>
            <div class="layui-input-inline" style="padding: 5px">
                <input type="text" name="adminName" autocomplete="off" class="layui-input layui-input-inline"
                       placeholder="请输入管理员姓名称" style="height: 30px;border-radius: 10px">
            </div>
        </div>
        <div class="layui-inline">
            <button type="button"
                    class="layui-btn layui-btn-normal layui-icon layui-icon-search layui-btn-radius layui-btn-sm"
                    lay-submit lay-filter="formDemo" style="margin-top: 4px">查询
            </button>
            <button type="reset"
                    class="layui-btn layui-btn-warm layui-icon layui-icon-refresh layui-btn-radius layui-btn-sm"
                    style="margin-top: 4px">重置
            </button>
        </div>
    </div>

</form>
<!-- 搜索条件结束 -->

<table id="adminTable" lay-filter="adminTable"></table>

<script id="toolbaradmin" type="text/html">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="adminAdd">添加</button>
    </div>
</script>

<script id="baradmin" type="text/html">
    <a class="layui-btn layui-btn-xs layui-btn-radius" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs " lay-event="reset">重置密码</a>
    {{#  if(d.adminId !=${admin.adminId}){ }}
     <a class="layui-btn layui-btn-danger layui-btn-xs " lay-event="del">删除</a>
        {{# } }}

</script>

<!-- 添加和修改的弹出层-->
<div style="display: none;padding: 20px" id="saveOrUpdateDiv">
    <form class="layui-form" lay-filter="dataFrm" id="dataFrm">
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">管理员姓名:</label>
                <div class="layui-input-inline">
                    <input type="text" name="adminName" lay-verify="required" placeholder="请输入管理员姓名" autocomplete="off"
                           class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <input type="hidden" id="adminId" name="adminId">
                <label class="layui-form-label">登录账号:</label>
                <div class="layui-input-inline">
                    <input type="text" name="loginId" id="loginId" lay-verify="required" placeholder="请输入登录账号" autocomplete="off"
                           class="layui-input " >
                </div>
            </div>
        </div>
        <div class="layui-form-item" id="d1">
            <div class="layui-inline">
                <label class="layui-form-label">密码:</label>
                <div class="layui-input-inline">
                    <input type="password" name="loginPwd" placeholder="请输入密码" autocomplete="off" class="layui-input" lay-verify="required">
                </div>
            </div>

        </div>


        <div class="layui-form-item">
            <div class="layui-input-block" style="text-align: center;padding-right: 120px">
                <button type="button"
                        class="layui-btn layui-btn-normal layui-btn-md layui-icon layui-icon-release layui-btn-radius"
                        lay-filter="doSubmit" lay-submit="">提交
                </button>
                <button type="reset"
                        class="layui-btn layui-btn-warm layui-btn-md layui-icon layui-icon-refresh layui-btn-radius">重置
                </button>
            </div>
        </div>
    </form>
</div>
<%-- 添加或者修改弹出框结束 --%>

<script src="/layui/layui.js"></script>
<script>
    var adminTable
    var index
    var url
    layui.use(['table','form','layer','jquery'], function() {
        var table = layui.table
        var form = layui.form
        var layer = layui.layer
        var $ = layui.jquery

        // 初始table
        adminTable = table.render({
            elem: "#adminTable"
            , url: "/admin/findAdmin"
            , page: {limit: 5, limits: [5, 10, 15]}
            , cellMinWidth: 80
            , toolbar: '#toolbaradmin'
            , cols: [[ //表头
                 {field: 'adminId', title: '管理员编号', fixed: 'center',templet:function (d) {
                      return d.adminId
                     }}
                , {field: 'loginId', title: '账号', fixed: 'center'}
                , {field: 'adminName', title: '姓名', fixed: 'center'}
                , {field: 'createTime', title: '注册时间', fixed: 'center'}
                , {fixed: 'right', title: '操作', toolbar: '#baradmin', width: 250}
            ]]
        })


        // 查询
        form.on("submit(formDemo)", function (data) {
            adminTable.reload({
                url: "/admin/findAdmin"
                , where: {
                    'adminName': data.field.adminName


                }
                , page: {
                    curr: 1
                }
            })
            return false;
        })
        // 头部工具栏添加事件
        table.on('toolbar(adminTable)', function (obj) {
            if (obj.event == "adminAdd") { // 点击新增按钮执行的内容
                adminAdd()
            }
        })
        //监听行工具事件
        table.on('tool(adminTable)', function (obj) {
            var data = obj.data
            if (obj.event == "edit") {
                menuUpdate(data)
            } else if (obj.event == "del") {
                del(data)
            }else if(obj.event=="reset"){
                reset(data)
            }
        })
         //重置密码
         function reset(data) {
             layer.confirm("您确定重置密码为123456么？", function (ind) {

                 $.post("/admin/resetAdminPwd", {"adminId": data.adminId}, function (d) {
                     layer.msg(d.msg)
                     // 刷新table数据
                     adminTable.reload()

                 })
                 layer.close(ind)
             })
         }
        function del(data) {


            layer.confirm("您确定要删除" + data.adminName + "管理员么？", function (ind) {

                $.post("/admin/delAdmin", {"adminId": data.adminId}, function (d) {
                    layer.msg(d.msg)
                    // 刷新table数据
                    adminTable.reload()

                })
                layer.close(ind)
            })


        }

        function menuUpdate(data) {
            index = layer.open({
                type: 1
                , title: "修改管理员"
                , content: $("#saveOrUpdateDiv")
                , area: ['800px', '440px']
                , success: function (layero, index) {
                    form.val("dataFrm", data)
                    $("#loginId").addClass("layui-btn-disabled")
                    $("#d1").hide()
                    url = "/admin/updateAdmin"
                }
            })
        }

        function adminAdd() {
            // 弹出框
            index = layer.open({
                type: 1 //
                , title: "添加管理员"
                , content: $("#saveOrUpdateDiv")
                , area: ['800px', '440px']
                , success: function (layero, index) {
                    // 页面数据要清除
                    $("#dataFrm")[0].reset()
                    $("#loginId").removeClass("layui-btn-disabled")
                    $("#d1").show()
                    url = "/admin/addAdmin"
                }
            })

        }

        // form表达提交
        form.on("submit(doSubmit)", function () {
            layer.confirm("您确定要提交该数据么", function (ind) {
                $.post(url, $("#dataFrm").serialize(), function (data) {
                    layer.msg(data.msg)
                    layer.close(index)
                    adminTable.reload()
                })
                layer.close(ind)
            })


        })
    })


</script>
</body>
</html>